package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.CarViewAdapter
import com.jt.assistant.adapter.CarViewDetailAdapter
import com.jt.assistant.bean.CarView
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.WaybillLisy
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_car_view_detail.*

/**
 * 落地车辆查看详情
 */
class CarViewDetailActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        CarViewDetailAdapter(
            ArrayList<WaybillLisy>()
        )
    }

    private val mSearchData = ArrayList<WaybillLisy>()

    override fun onResume() {
        super.onResume()
        val params: Param = Param::class.java.newInstance()
        params.dispatchcarId = intent.getStringExtra("Id").toString()
        viewModel.carWaybillList(params)
    }

    override fun layoutId(): Int = R.layout.activity_car_view_detail

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_carViewDetail) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
    }

    override fun initListener() {
        tv_carViewSearchType.setOnClickListener {
            showSearchDialog()
        }

        tv_carViewSearchMDC.setOnClickListener {
            showSearchMDCDialog()
        }

        set_carViewSearchText.setOnScanListener {
            if (it.isNullOrEmpty()) {
                val params: Param = Param::class.java.newInstance()
                params.dispatchcarId = intent.getStringExtra("Id").toString()
                viewModel.carWaybillList(params)
                return@setOnScanListener
            }

            mAdapter.data.clear()
            mSearchData.forEach {
                if (it.channelOrderNo.contains(set_carViewSearchText.string)) {
                    mAdapter.data.add(it)
                }
            }
            mAdapter.notifyDataSetChanged()
            if (mAdapter.data.isEmpty()) ToastUtils.showShort("未搜索到数据")
        }


        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (mAdapter.data[position].checkStateStr == "已验收") {
                ToastUtils.showShort("渠道单号${mAdapter.data[position].checkStateStr}已验收")
                return@setOnItemClickListener
            }
            startActivity(Intent(this@CarViewDetailActivity, ReceiveActivity::class.java).apply {
                putExtra("CHANNELORDERNO", mAdapter.data[position].channelOrderNo)
            })


        }

        viewModel.mCarWaybillList.observe(this, {
            if (it.isSuccess()) {
                if (it.data.waybillLisy.isNullOrEmpty()) ToastUtils.showShort("没有数据")
                mSearchData.clear()
                mSearchData.addAll(it.data.waybillLisy)
                mAdapter.data.clear()
                mSearchData.forEach {
                    if (it.checkStateStr == "未验收") {
                        mAdapter.data.add(0, it)
                    } else {
                        mAdapter.data.add(it)
                    }
                }
                mAdapter.notifyDataSetChanged()
            }
        })
    }


    private fun showSearchDialog() {
        val list = listOf(
            "渠道号",
            "目的仓"
        )

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CarViewDetailActivity.tv_carViewSearchType.text = text

                if (index == 0) {//渠道号
                    this@CarViewDetailActivity.set_carViewSearchText.visibility = View.VISIBLE
                    this@CarViewDetailActivity.tv_carViewSearchMDC.visibility = View.GONE

                    val params: Param = Param::class.java.newInstance()
                    params.dispatchcarId = intent.getStringExtra("Id").toString()
                    viewModel.carWaybillList(params)

                } else {//目的仓
                    this@CarViewDetailActivity.set_carViewSearchText.visibility = View.GONE
                    this@CarViewDetailActivity.tv_carViewSearchMDC.visibility = View.VISIBLE
                    showSearchMDCDialog()
                }
            }
            lifecycleOwner(this@CarViewDetailActivity)
        }
    }


    private fun showSearchMDCDialog() {
        val list = ArrayList<String>()
        mSearchData.forEach {
            if (!list.contains(it.receiveAddress)) {
                list.add(it.receiveAddress)
            }
        }

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CarViewDetailActivity.tv_carViewSearchMDC.text = text
                mAdapter.data.clear()
                mSearchData.forEach {
                    if (it.receiveAddress == text) {
                        mAdapter.data.add(it)
                    }
                }
                mAdapter.notifyDataSetChanged()

            }
            lifecycleOwner(this@CarViewDetailActivity)
        }
    }

}